package com.yumeko.service.impl;

import com.yumeko.entity.TOrder;
import com.yumeko.dao.TOrderDao;
import com.yumeko.service.TAccountService;
import com.yumeko.service.TOrderService;
import com.yumeko.service.TStorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * (TOrder)表服务实现类
 *
 * @author Zheng Hao
 * @since 2020-03-20 19:31:45
 */
@Service("tOrderService")
@Slf4j
public class TOrderServiceImpl implements TOrderService {
    @Resource
    private TOrderDao tOrderDao;
    @Resource
    private TAccountService accountService;
    @Resource
    private TStorageService storageService;

    @Override
    @GlobalTransactional(name = "fsp-create-order",rollbackFor = Exception.class)
    public void create(TOrder tOrder) {
        log.info("--------->开始新建订单");
        //1 新建订单
        tOrderDao.create(tOrder);

        //2 扣减库存
        log.info("------------->订单微服务开始调用库存,做扣减Count");
        storageService.decrease(tOrder.getProductId(), tOrder.getCount());
        log.info("------------->订单微服务开始调用库存,做扣减end");

        //3 扣减账户
        log.info("------------->订单微服务开始调用账户,做扣减Money");
        accountService.decrease(tOrder.getUserId(), BigDecimal.valueOf(tOrder.getMoney()));
        log.info("------------->订单微服务开始调用账户,做扣减end");

        //4 修改订单状态
        log.info("------------->修改订单状态开始");
        tOrderDao.update(tOrder.getUserId(),0);
        log.info("------------->修改订单状态结束");

        log.info("------------->下订单结束了");
    }
}